package com.aaa.sbms.service;

import com.aaa.sbms.dao.CarDao;
import com.aaa.sbms.util.SessionUtil;
import com.aaa.sbms.util.UserSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CarServiceImpl implements CarService {

    @Autowired(required = false)
    private CarDao carDao;

    @Override
    public Map cars() {
        Map resultMap = new HashMap();
        List<Map> list = carDao.cars();
        resultMap.put("list",list);
        return resultMap;
    }

    @Override
    public Map carPage(Map map) {
        int pageNo = Integer.valueOf(map.get("pageNo").toString());
        int pageSize = Integer.valueOf(map.get("pageSize").toString());
        int begin = (pageNo-1)*pageSize;
        map.put("begin",begin);
        map.put("pageSize",pageSize);
        List<Map> page = carDao.carPage(map);
        int total = carDao.carPageCount(map);
        Map resultMap= new HashMap();
        resultMap.put("list",page);
        resultMap.put("total",total);
        return resultMap;
    }

    @Override
    public Map carauditPage(Map map) {
        int pageNo = Integer.valueOf(map.get("pageNo").toString());
        int pageSize = Integer.valueOf(map.get("pageSize").toString());
        int begin = (pageNo-1)*pageSize;
        map.put("begin",begin);
        map.put("pageSize",pageSize);
        List<Map> page = carDao.carauditPage(map);
        int total = carDao.carauditPageCount(map);
        Map resultMap= new HashMap();
        resultMap.put("list",page);
        resultMap.put("total",total);
        return resultMap;
    }

    @Override
    public Map carusePage(Map map) {
        int pageNo = Integer.valueOf(map.get("pageNo").toString());
        int pageSize = Integer.valueOf(map.get("pageSize").toString());
        int begin = (pageNo-1)*pageSize;
        map.put("begin",begin);
        map.put("pageSize",pageSize);
        List<Map> page = carDao.carusePage(map);
        int total = carDao.carusePageCount(map);
        Map resultMap= new HashMap();
        resultMap.put("list",page);
        resultMap.put("total",total);
        return resultMap;
    }

    @Override
    public Map carmaintainPage(Map map) {
        int pageNo = Integer.valueOf(map.get("pageNo").toString());
        int pageSize = Integer.valueOf(map.get("pageSize").toString());
        int begin = (pageNo-1)*pageSize;
        map.put("begin",begin);
        map.put("pageSize",pageSize);
        List<Map> page = carDao.maintainCarPage(map);
        int total = carDao.maintainCarPageCount(map);
        Map resultMap= new HashMap();
        resultMap.put("list",page);
        resultMap.put("total",total);
        return resultMap;
    }

    @Override
    public Map carrefuelPage(Map map) {
        int pageNo = Integer.valueOf(map.get("pageNo").toString());
        int pageSize = Integer.valueOf(map.get("pageSize").toString());
        int begin = (pageNo-1)*pageSize;
        map.put("begin",begin);
        map.put("pageSize",pageSize);
        List<Map> page = carDao.carRefuelPage(map);
        int total = carDao.carRefuelPageCount(map);
        Map resultMap= new HashMap();
        resultMap.put("list",page);
        resultMap.put("total",total);
        return resultMap;
    }

    @Override
    public Map carOne(int carid) {
        Map resultMap = new HashMap();
        Map list = carDao.carOne(carid);
        resultMap.put("list",list);
        return resultMap;
    }

    @Override
    public Map carauditOne(int id) {
        Map resultMap = new HashMap();
        Map list = carDao.carauditOne(id);
        resultMap.put("list",list);
        return resultMap;
    }

    @Override
    public Map caruseOne(int id) {
        Map resultMap = new HashMap();
        Map list = carDao.caruseOne(id);
        resultMap.put("list",list);
        return resultMap;
    }

    @Override
    public int addCar(Map map) {
//        int userId = SessionUtil.getUserId();//当前用户
//        map.put("userid",userId);
        return carDao.addCar(map);
    }

    @Override
    public int addCarAudit(Map map) {
        Map user = (Map) UserSession.getUser();
        int userId = Integer.valueOf(user.get("userid").toString());
        map.put("userid",userId);
        return carDao.addCarAudit(map);
    }

    @Override
    public int addCarMaintain(Map map) {
        int userId = SessionUtil.getUserId();//当前用户
        map.put("userid",userId);
        return carDao.addCarMaintain(map);
    }

    @Override
    public int addCarRefuel(Map map) {
        int userId = SessionUtil.getUserId();//当前用户
        map.put("userid",userId);
        return carDao.addCarRefuel(map);
    }

    @Override
    public int updateCar(Map map) {
        return carDao.updateCar(map);
    }

    @Override
    public int deleteCar(int carid) {
        return carDao.deleteCar(carid);
    }

    @Override
    @Transactional
    public int audit(Map map) {//id userid username carid cartype carplate usereason auditstate
        try {
            int carid = Integer.valueOf(map.get("carid").toString());
            Map map1 = carDao.carOne(carid);
            int state = Integer.valueOf(map1.get("state").toString());
            if (state==2){
                return 0;
            }
            int auditstate=Integer.valueOf(map.get("state").toString());
            int audituserid = SessionUtil.getUserId();
            map.put("audituserid",audituserid);
            if (auditstate==3){//通过
                //修改申请表的开始时间、审核状态、审核人
                carDao.updateCarAuditAsPass(map);
                // 修改车辆表对应的车辆状态、使用者
                carDao.updateCarStateAsUsing(map);
                // 添加车辆使用记录
                carDao.addCarUseAfterLend(map);
            }
            if (auditstate==2){//驳回
                //修改申请表的审核状态、审核人
                carDao.updateCarAuditAsReject(map);
            }
            return 1;
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
    }
}
